/* Code licensed under the BSD License: http://www.opensource.org/licenses/bsd-license.php */
/**
* @description <p>Widgets used for page layout.</p>
**/

// var tabView = null;
var DIV_LISTER_INDEX = 0;
var DIV_EDITOR_INDEX = 1;

if (typeof MENU == "undefined") {
    var MENU = {};
}

MENU.WorkflowHint = function () {
    ANVIL.setMessage("Workflow error. Select row and try again.");
};

MENU.onProfileReady = function () {
    YAHOO.util.Event.onAvailable("listerMenu", MENU.initListerMenu); 
    YAHOO.util.Event.onAvailable("editorMenu", MENU.initEditorMenu);     
};

MENU.isEnabled = function (oButton) {
    // ISSUE: Under IE, the button beta is letting clicks through
  var disabled = oButton.get("disabled");
  return (!disabled);
};

MENU.refreshButton;
MENU.refreshEvent = new YAHOO.util.CustomEvent("refreshEvent",MENU);
MENU.onRefresh = function (e) {
    MENU.refreshEvent.fire(e);
};

MENU.editButton;
MENU.editEvent = new YAHOO.util.CustomEvent("editEvent",MENU);
MENU.onEdit = function (e) {
    if (MENU.isEnabled(MENU.editButton)) {
        MENU.editEvent.fire(e);
    }
};

MENU.addButton;
MENU.addEvent = new YAHOO.util.CustomEvent("addEvent",MENU);
MENU.onAdd = function (e) {
    if (MENU.isEnabled(MENU.addButton)) {
        MENU.addEvent.fire(e);
    }        
};

MENU.listerDeleteButton;
MENU.listerDeleteEvent = new YAHOO.util.CustomEvent("listerDeleteEvent",MENU);
MENU.onListerDelete = function (e) {
    if (MENU.isEnabled(MENU.listerDeleteButton)) {
        MENU.listerDeleteEvent.fire(e);
    }    
};

MENU.isEditor = function () {
    var editor = false;    
    if (typeof PROFILE == "undefined") { 
        editor = true;
    } else if (PROFILE.isEditor()) {
        editor = true;
    }
    return editor;
};

MENU.createOrDisableListerButton = function (bEditor,oButton,sLabel,fHandler) {

    var bUndefined = (typeof oButton == "undefined");
    var bCreateButton = ((bEditor) && (bUndefined));   
    var bDisableButton = ((!bEditor) && (!bUndefined));
    
    if (bCreateButton) {
        oButton = new YAHOO.widget.Button(
          { label:sLabel, container:"listerMenu", onclick: { fn: fHandler } });        
    }
    
    if (bDisableButton) {
        oButton.set("disabled",true);
    }
    
    return oButton;
};

MENU.initListerMenu = function () {  

    var bNeedRefresh = (typeof MENU.refreshButton == "undefined");
    if (bNeedRefresh) {
        MENU.refreshButton = new YAHOO.widget.Button(
            { label:"Refresh", container:"listerMenu", onclick: { fn: MENU.onRefresh } });        
    }
    
    var bEditor = MENU.isEditor();    
    MENU.editButton = 
        MENU.createOrDisableListerButton(bEditor, MENU.editButton, "Edit", MENU.onEdit );
    MENU.addButton = 
        MENU.createOrDisableListerButton(bEditor, MENU.addButton, "Add", MENU.onAdd );
    MENU.listerDeleteButton = 
        MENU.createOrDisableListerButton(bEditor, MENU.listerDeleteButton, "Delete", MENU.onListerDelete );
};

MENU.saveButton;
MENU.saveEvent = new YAHOO.util.CustomEvent("saveEvent",MENU);
MENU.onSave = function (e) {
    if (MENU.isEnabled(MENU.saveButton)) {
        MENU.saveEvent.fire(e);
    }
};

MENU.editorDeleteButton;
MENU.editorDeleteEvent = new YAHOO.util.CustomEvent("editorDeleteEvent",MENU);
MENU.onEditorDelete = function (e) {
    if (MENU.isEnabled(MENU.editorDeleteButton)) {
        MENU.editorDeleteEvent.fire(e);
    }
};

MENU.cancelButton ;
MENU.cancelEvent = new YAHOO.util.CustomEvent("cancelEvent",MENU);
MENU.onCancel = function (e) {
    if (MENU.isEnabled(MENU.cancelButton)) {    
        MENU.cancelEvent.fire(e);
    }
};

MENU.initEditorMenu = function () {    

    if (!(typeof MENU.saveButton == "undefined")) return;

    MENU.saveButton = new YAHOO.widget.Button( 
        { label: "Save", container: "editorMenu", onclick: { fn: MENU.onSave }} );

    MENU.editorDeleteButton = new YAHOO.widget.Button( 
        { label: "Delete", container: "editorMenu", onclick: { fn: MENU.onEditorDelete }} );

    MENU.cancelButton = new YAHOO.widget.Button( 
        { label: "Cancel", container: "editorMenu", onclick: { fn: MENU.onCancel }} );   
        
};                

MENU.init = function () {    
    // Setup listeners to clear message area 
    YAHOO.util.Event.addListener("doc3", "click", ANVIL.yui.onClear);
    YAHOO.util.Event.addListener("doc3", "dblclick", ANVIL.yui.onClear);
    YAHOO.util.Event.addListener("doc3", "keypress", ANVIL.yui.onClear);    
};

MENU.onListerRowSelected = function(oArgs) {
    if (MENU.isEditor()) {
         MENU.editButton.set("disabled",false);
         MENU.listerDeleteButton.set("disabled",false);
    }
};

MENU.onListerRowUnselected = function(oArgs) {
    if (MENU.isEditor()) {
         MENU.editButton.set("disabled",true);
         MENU.listerDeleteButton.set("disabled",true);
    }
};
